package com.syni.mdd.sjb.community.base.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syni.mdd.sjb.common.component.mybatis.community.entity.SjbQywxTagInfo;
import com.syni.mdd.sjb.common.component.mybatis.community.mapper.SjbQywxTagInfoMapper;
import com.syni.mdd.sjb.common.component.utils.common.exception.BusinessException;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.sjb.community.base.constants.Constants;
import com.syni.mdd.sjb.community.base.entity.vo.QywxCorpTagVO;
import com.syni.mdd.sjb.community.base.service.SjbQywxCompanyService;
import com.syni.mdd.sjb.community.base.service.SjbQywxTagInfoService;
import com.syni.mdd.sjb.community.base.utils.CommonBeanUtils;
import com.syni.mdd.sjb.community.base.utils.OAUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author wangbaitao
 * @version 1.0.0
 * <h>企业微信标签数据表实体service impl</h>
 * @Date 2021/3/1
 **/
@Service
@Transactional
public class SjbQywxTagInfoServiceImpl extends ServiceImpl<SjbQywxTagInfoMapper, SjbQywxTagInfo>
        implements SjbQywxTagInfoService {
    @Autowired
    private SjbQywxCompanyService sjbQywxCompanyService;

    @Override
    public Map<String, Object> syncData(String corpId) {
        JSONObject jsonObj = OAUtils.queryCompanyTagInfos(CommonBeanUtils.getToken(corpId), null,
                null, corpId);
        if (Objects.equals(Constants.QYWX_OK, jsonObj.getInteger(Constants.ERROR_CODE))) {
            //进行数据的删除
            remove(new QueryWrapper<SjbQywxTagInfo>().eq(SjbQywxTagInfo.CORP_ID, corpId));
            //进行数据的解析和加工
            List<QywxCorpTagVO> groupTagInfos = JSONArray.parseArray(jsonObj.getString(Constants.TAG_GROUP),
                    QywxCorpTagVO.class);
            List<SjbQywxTagInfo> tagInfos = new ArrayList<>();
            LocalDateTime syncTime = LocalDateTime.now();
            groupTagInfos.forEach(groupTag -> {
                groupTag.getTag().forEach(tag -> {
                    SjbQywxTagInfo tagInfo = new SjbQywxTagInfo();
                    tagInfo.setCorpId(corpId);
                    tagInfo.setSyncTime(syncTime);
                    tagInfo.setGroupName(groupTag.getGroup_name());
                    tagInfo.setGroupId(groupTag.getGroup_id());
                    tagInfo.setCreateTime(LocalDateTime.ofEpochSecond(groupTag.getCreate_time(), 0,
                            ZoneOffset.ofHours(8)));
                    tagInfo.setGroupOrder(groupTag.getOrder());
                    tagInfo.setTagId(tag.getId());
                    tagInfo.setTagName(tag.getName());
                    tagInfo.setTagOrder(tag.getOrder());
                    tagInfo.setTagCreateTime(LocalDateTime.ofEpochSecond(tag.getCreate_time(), 0,
                            ZoneOffset.ofHours(8)));
                    //加工完成
                    tagInfos.add(tagInfo);
                });
            });
            saveBatch(tagInfos);
        } else {
            throw new BusinessException("业务异常，获取企业标签数据失败:" + jsonObj.getString(Constants.ERROR_MSG));
        }
        return ResultMapHelper.success(null, "同步成功");
    }
}
